package com.nf.health.app.myfamily.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.OldCallAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.utils.ViewUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
 * @ClassName: OldSettongCallActivity 
 * @Description: 老人机设置快速拨号
 * @author Ian 
 * @date 2015-11-18 下午3:19:31 
 *  
 */
public class OldSettongCallActivity extends BaseActivity {


	private Map<String, String> isCheckMap;
	private Map<String, String> isCheckMapadd = new HashMap<String, String>();

	private Button next_btn;

	private EditText num,phonenum,name;

	private MyListView oldcall_listview;

	String name1,num1,phonenum1;

	private LinearLayout oldcall_lin;

	private String imei;

	List list = new ArrayList();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_old_settong_call);
		ViewUtil.setText(this, R.id.titlebar_title_tv, "设置快速拨号");
		initView();

		imei = getIntent().getStringExtra("imei");
		provider.requestOldCall(imei,"OldCall");


	}

	private void initView() {
		// TODO Auto-generated method stub
		next_btn = (Button) findViewById(R.id.next_btn);//保存按钮

		name = (EditText) findViewById(R.id.old_ed_name);//联系人名称
		num = (EditText) findViewById(R.id.old_ed_num);//快捷键
		phonenum = (EditText) findViewById(R.id.old_ed_phonenum);//电话号码

		oldcall_lin = (LinearLayout) findViewById(R.id.oldcall_lin);

		oldcall_listview = (MyListView) findViewById(R.id.oldcall_listview);//数据列表



		next_btn.setOnClickListener(new OnClickListener() {//保存联系人

			@SuppressWarnings("unchecked")
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				name1 = name.getText().toString();
				num1 = num.getText().toString();
				phonenum1 = phonenum.getText().toString();



				if(TextUtils.isEmpty(name1)){
					showToast("联系人不可为空");
				}else if(TextUtils.isEmpty(num1)){
					showToast("快捷键号不可为空");
				}else if(TextUtils.isEmpty(phonenum1)){
					showToast("手机号不可为空");
				}else if(Integer.parseInt(num1) > 9){
					showToast("快捷键号最大为9");
				}else if(Integer.parseInt(num1) < 1){
					showToast("快捷键号最小为1");
				}else if(phonenum1.length() != 11){
					showToast("手机号格式不正确");
				}else{

					String exist = list+"";
					if(exist.contains("\"no\""+"="+"\""+num1+"\"")){
						showToast("该快捷键号已被占用");
					}else{
						isCheckMapadd.put("\"no\"","\""+num1+"\"");
						isCheckMapadd.put("\"name\"","\""+name1+"\"");
						isCheckMapadd.put("\"num\"","\""+phonenum1+"\"");
						List list1 = new ArrayList();
						
						if(list.equals("[]")){
							
						}else{
							list1.addAll(list);
						}
						list1.add(isCheckMapadd);//Integer.parseInt(num1)
						String data =list1+"";
						provider.requestOldUpdatePhone(imei,data.replace(" ","").replace("=",":"),"quick","OldUpdatePhone");
						
						OldCallAdapter adapter = new OldCallAdapter(OldSettongCallActivity.this,list1);
						oldcall_listview.setAdapter(adapter);
						
					}

				}

			}
		});

	}


	@SuppressWarnings("unchecked")
	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("OldCall")) {

			if("{}".equals(object)){
				showToast("无数据 请添加数据");
			}else{

				try {
					JSONObject jsonObject = new JSONObject(object+"");

					JSONArray jsonArray = jsonObject.getJSONArray("quick");
					for(int i = 0;i < jsonArray.length();i++){

						String jsonarray = jsonArray.getString(i);

						JSONObject jsonObject2 = new JSONObject(jsonarray);
						String no = jsonObject2.getString("no");
						String num = jsonObject2.getString("num");
						String name = jsonObject2.getString("name");
						isCheckMap = new HashMap<String, String>();
						isCheckMap.put("\"no\"","\""+no+"\"");
						isCheckMap.put("\"num\"","\""+num+"\"");
						isCheckMap.put("\"name\"","\""+name+"\"");

						if(num.equals("") && name.equals("")){
							
						}else{
							list.add(isCheckMap);
						}
						
					}


				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}


				if(list.size()>8){
					oldcall_lin.setVisibility(View.GONE);
				}


				final OldCallAdapter adapter = new OldCallAdapter(OldSettongCallActivity.this,list);
				oldcall_listview.setAdapter(adapter);

				adapter.setOnMyClickListener(new OnMyClickListener() {

					@Override
					public void onClick(int position, String flag) {
						// TODO Auto-generated method stub
						list.remove(position);
//						list.clear();
						adapter.notifyDataSetChanged();
						String data = list+"";
						provider.requestOldUpdatePhone(imei,data.replace(" ","").replace("=",":"),"quick","DelUpdatePhone");
						if(list.size()<9){
							oldcall_lin.setVisibility(View.VISIBLE);
						}

					}
				});

			}


		}
		if(actionName.equals("OldUpdatePhone")){


			if("true".equals(object)){
				showToast("添加快捷拨号成功");
				name.setText("");
				num.setText("");
				phonenum.setText("");
			}

		}
		if(actionName.equals("DelUpdatePhone")){
			if("true".equals(object)){
				showToast("删除成功");
//				provider.requestOldCall(imei,"OldCall");
			}

		}
	}

}
